$.validator.setDefaults({
    submitHandler: function () {
        login();
    }
});


$(function () {
    ilabLogin();
    /*checkToken();*/
    validateKickout();
    // PsRole();
    // validateRule();
    $('.imgcode').click(function () {
        var url = ctx + "captcha/captchaImage?type=" + captchaType + "&s=" + Math.random();
        $(".imgcode").attr("src", url);
    });
})

function submitLogin() {

    // $("input[name='username']").val("adminlzb");
    // $("input[name='password']").val("12345678");
    $.ajax({
        type: "post",
        url: ctx + "Tslogin",
        data: {


        },
        success: function (r) {
            if (r.code == 0) {
                location.href = ctx + 'index';
            } else {
                $.modal.closeLoading();
                $('.imgcode').click();
                $(".code").val("");
                $.modal.msg(r.msg);
            }
        }
    });

}

function login() {
    console.log("login------------")
    $.modal.loading($("#btnSubmit").data("loading"));
    var username = $.common.trim($("input[name='username']").val());
    var password = $.common.trim($("input[name='password']").val());
    var validateCode = $("input[name='validateCode']").val();
    var rememberMe = $("input[name='rememberme']").is(':checked');
   sessionStorage.setItem("name",$("input[name='username']").val())
    console.log(sessionStorage.getItem("name"));
    $.ajax({
        type: "post",
        url: ctx + "login",
        data: {
            "username": username,
            "password": password,
            "validateCode": validateCode,
            "rememberMe": rememberMe
        },
        success: function (r) {
            if (r.code == 0) {
                location.href = ctx + 'index';
            } else {
                $.modal.closeLoading();
                $('.imgcode').click();
                $(".code").val("");
                $.modal.msg(r.msg);
            }
        }
    });

}

function PsRole1(val) {
    $.validator.addMethod('validateName',function(value1, element){
        if(value1 === ''){
            return false
        }
        return true
    }, val==='1'? "评审账户为:评审账户":'请输入您的用户名')

    $.validator.addMethod('validatePassWord',function(value1, element){
        if(value1 === ''){
            return false
        }
        return true
    }, val==='1'? "评审密码为 Ps*154412021":'请输入您的密码')
    var icon = "<i class='fa fa-times-circle'></i> ";
    $("#signupForm").validate({
        rules: {
            username: {
                validateName: true
            },
            password: {
                validatePassWord: true
            }
        },
        messages: {
            // username: {
            //     required: "请输入您的用户名",
            // },
            // password: {
            //     required: "请输入您的密码",
            // }
        }
    })
    // if($('#username-error')&&val==1){
    //     console.log('---')
    //     $('#username-error').text('评审账户为:评审账户')
    //     $('#password-error').text('评审密码为 Ps*154412021')
    // }
}

// 显示账号密码
function showAccount() {
    console.log('显示账号密码')
    $('.pass-username').addClass('show-text')
    $('.pass-password').addClass('show-text')
}

function PsRole(val) {
    console.info(val)
    var icon = "<i class='fa fa-times-circle'></i> ";
    $("#signupForm").validate({
        rules: {
            username: {
                required: true
            },
            password: {
                required: true
            }
        },
        messages: {
            username: {
                required: "评审账户为:评审账户",
            },
            password: {
                required: "评审密码为 Ps*154412021",
            }
        }
    })
}

function validateRule() {
    console.info('------------------------')
    var icon = "<i class='fa fa-times-circle'></i> ";
    $("#signupForm").validate({
        rules: {
            username: {
                required: true
            },
            password: {
                required: true
            }
        },
        messages: {
            username: {
                required: "请输入您的用户名",
            },
            password: {
                required: "请输入您的密码",
            }
        }
    })
}

function validateKickout() {
    if (getParam("kickout") == 1) {
        layer.alert("<font color='red'>您已在别处登录，请您修改密码或重新登录</font>", {
                icon: 0,
                title: "系统提示"
            },
            function (index) {
                //关闭弹窗
                layer.close(index);
                if (top != self) {
                    top.location = self.location;
                } else {
                    var url = location.search;
                    if (url) {
                        var oldUrl = window.location.href;
                        var newUrl = oldUrl.substring(0, oldUrl.indexOf('?'));
                        self.location = newUrl;
                    }
                }
            });
    }
}


function getParam(paramName) {
    var reg = new RegExp("(^|&)" + paramName + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return decodeURI(r[2]);
    return null;
}

function ilabLogin() {
    console.log("进入方法")
    /**
     * 获取前端存放的解析后的信息
     */
    var data = window.sessionStorage.getItem("GTX_DATA");
    console.log(data)
    if (data == undefined || data == "" || data == '') {
        $.modal.msg("未解析出用户信息，请从ilab登录进入，或者直接输入令牌进入");
        return;
    }
    var ilabPojo = JSON.parse(data);
    if (ilabPojo.code != 0) {
        $.modal.msg("token解析失效，请从ilab登录进入，或者直接输入令牌进入");
    } else {
        $.ajax({
            type: "post",
            url: ctx + "ilab/login",
            contentType: "application/json",
            data: data,
            success: function (r) {
                if (r.code == 0) {
                    location.href = ctx + 'index';
                } else {
                    $.modal.closeLoading();
                    $('.imgcode').click();
                    $(".code").val("");
                    $.modal.msg(r.msg);
                }
            }
        });
    }
}


// 检查地址栏是否携带token
function checkToken() {
    var str = window.location.href.split("?");
    var params = str.length > 0 ? str[1] : "";
    var obj = {}; // 用一个对象存储目标值
    if (params) {
        var param = params.split("&");
        for (var i = 0; i < param.length; i++) {
            // 循环遍历截取出来的param数组
            var paramsA = param[i].split("=");
            var key = paramsA[0];
            var value = paramsA[1];
            obj[key] = value;
        }
    }
    if (obj["ticket"]) {
        $.ajax({
            type: "post",
            url: ctx + "ilab/login",
            data: {
                "token": obj["ticket"]
            },
            success: function (r) {
                if (r.code == 0) {
                    location.href = ctx + 'index';
                } else {
                    $.modal.closeLoading();
                    $('.imgcode').click();
                    $(".code").val("");
                    $.modal.msg(r.msg);
                }
            }
        });
    } else {
        window.console.log("未发现token", obj);
    }
}